package base.httpClient;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class WeiBo {
	// The HttpClient is used in one session
	private HttpResponse response;
	private DefaultHttpClient httpclient = new DefaultHttpClient();

	private String getText(String redirectLocation) {
		HttpGet httpget = new HttpGet(redirectLocation);
		// Create a response handler
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = "";
		try {
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (Exception e) {
			e.printStackTrace();
			responseBody = null;
		} finally {
			httpget.abort();
			httpclient.getConnectionManager().shutdown();
		}
		return responseBody;
	}

	public void printText() throws IOException {
		File file = new File("D://taobao-index.html");
		FileOutputStream outputStream = new FileOutputStream(file);
		OutputStreamWriter writer = new OutputStreamWriter(outputStream, "GBK");
		BufferedWriter bufferedWriter = new BufferedWriter(writer);
		bufferedWriter.write(getText("http://www.taobao.com"));
		bufferedWriter.flush();
		outputStream.close();
		bufferedWriter.close();
	}

	public static void main(String[] args) {
		WeiBo wb = new WeiBo();
		try {
			wb.printText();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}